import os
import requests
from pathlib import Path

# 定义需要下载的资源
RESOURCES = {
    'css': {
        'bootstrap': [
            'https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.0/css/bootstrap.min.css',
            'https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.0/css/bootstrap.min.css.map'
        ],
    },
    'js': {
        'bootstrap': [
            'https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.0/js/bootstrap.bundle.min.js',
            'https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.0/js/bootstrap.bundle.min.js.map'
        ],
        'jquery': [
            'https://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.min.js'
        ],
    }
}

def download_file(url, destination):
    response = requests.get(url)
    if response.status_code == 200:
        os.makedirs(os.path.dirname(destination), exist_ok=True)
        with open(destination, 'wb') as f:
            f.write(response.content)
        print(f"Downloaded: {destination}")
    else:
        print(f"Failed to download: {url}")

def main():
    base_path = Path(__file__).parent.parent / 'static'
    
    # 创建必要的目录
    for folder in ['css/vendor', 'js/vendor', 'fonts', 'images']:
        (base_path / folder).mkdir(parents=True, exist_ok=True)
    
    # 下载资源
    for resource_type, resources in RESOURCES.items():
        for name, urls in resources.items():
            if not isinstance(urls, list):
                urls = [urls]
            for url in urls:
                file_name = url.split('/')[-1]
                destination = base_path / resource_type / 'vendor' / file_name
                download_file(url, destination)
    
    # 创建空的 main.js 和 style.css
    (base_path / 'js' / 'main.js').touch()
    if not (base_path / 'css' / 'style.css').exists():
        with open(base_path / 'css' / 'style.css', 'w', encoding='utf-8') as f:
            f.write('/* Custom styles */\n')

if __name__ == '__main__':
    main() 